package com.wondertek.common.log;

import cn.hutool.extra.spring.SpringUtil;
import com.wondertek.common.util.base.AddressUtils;
import com.wondertek.common.util.base.IpUtils;
import com.wondertek.common.util.security.LoginUser;
import com.wondertek.common.util.security.SecurityUtil;

import java.util.Date;

/**
 * @author xqs
 */
public class AutoLogUtil {

    public static void save(String category,String handleType,String handleNote){
        LoginUser loginUser = SecurityUtil.getDefaultLoginUser();
        save(loginUser, category, handleType, handleNote,null,null, null,null);
    }

    /**
     * 站点设备日志
     *
     * @param category
     * @param handleType
     * @param handleNote
     * @param siteName
     * @param deviceId
     */
    public static void save(String category,String handleType,String handleNote, String siteName,String deviceId,String siteId,String accessoryId){
        LoginUser loginUser = SecurityUtil.getDefaultLoginUser();
        save(loginUser, category, handleType, handleNote, siteName, deviceId, siteId, accessoryId);
    }

    public static void save(LoginUser loginUser ,String category,String handleType,String handleNote, String siteName,String deviceId,String siteId,String accessoryId){
        AutoLogDto autoLogDto = new AutoLogDto();
        autoLogDto.setCategory(category);
        autoLogDto.setHandleType(handleType);
        autoLogDto.setHandleNote(handleNote);
        String ip = IpUtils.getIpAddr();
        autoLogDto.setHandleIp(IpUtils.getIpAddr());
        autoLogDto.setHandleTime(new Date());
        autoLogDto.setUserId(loginUser.getUserId());
        autoLogDto.setUsername(loginUser.getUsername());
        autoLogDto.setUserInfo((loginUser.getUserInfo()));
        autoLogDto.setSiteName(siteName);
        autoLogDto.setDeviceId(deviceId);
        autoLogDto.setSiteId(siteId);
        autoLogDto.setAccessoryId(accessoryId);
        autoLogDto.setAddress(AddressUtils.getRealAddressByIP(ip));
        save(autoLogDto);
    }

    public static void save(AutoLogDto autoLogDto){
        SpringUtil.getBean(AutoLogService.class).saveAutoLog(autoLogDto);
    }
}
